import { goodsPage } from '../../../api/sale'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    requestData: {
      page: 1,
      size: 4,
      typeId: '',
      typePid: '',
      typeName: '',
      order: []
    },
    isFlush: false,
    isReadyLoad: true,
    isLoading: false,
    goodsList: [],
    sortUpDown: null,
    screenStyle: 1,
    boldStr: 'ALL'
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onLoad(options: any) {
    Object.assign(this.data.requestData, options)
    wx.setNavigationBarTitle({
      title: this.data.requestData.typeName,
    })
    this.getGoodsPage()
  },
  onPullDownRefresh() {
    if (this.data.isLoading) return
    this.setData({
      "requestData.page": 1,
      isLoading: true
    })
    goodsPage(this.data.requestData).then(res => {
      this.setData({
        goodsList: res.data.records,
        isReadyLoad: res.data.total <= res.data.size * this.data.requestData.page,
        isLoading: false
      })
      wx.stopPullDownRefresh()
    })
  },
  onReachBottom() {
    if (this.data.isLoading) return
    if (this.data.isFlush || this.data.isReadyLoad) return
    this.setData({
      "requestData.page": this.data.requestData.page + 1,
      isLoading: true
    })

    goodsPage(this.data.requestData).then(res => {
      this.setData({
        goodsList: this.data.goodsList.concat(res.data.records),
        isReadyLoad: res.data.total <= res.data.size * this.data.requestData.page,
        isLoading: false
      })
    })
  },
  getGoodsPage() {
    goodsPage(this.data.requestData).then(res => {
      this.setData({
        goodsList: res.data.records,
        isFlush: res.data.total === 0,
        isReadyLoad: res.data.total <= res.data.size * this.data.requestData.page
      })
    })
  },

  //**************************以下是排序的各个方法*************
  //综合排序
  sortByObject(event: any) {
    this.setData({
      goodsList: [],
      "requestData.page": 1,
      boldStr: event.currentTarget.dataset.type
    })
    if (event.currentTarget.dataset.type === 'ALL') {
      this.getGoodsPage()
      return;
    }
    const data = [{
      column: event.currentTarget.dataset.type,
      asc: event.currentTarget.dataset.type === 'price' ? (this.data.sortUpDown % 2 == 0) : false
    }]
    this.setData({
      sortUpDown: event.currentTarget.dataset.type === 'price' ? (!this.data.sortUpDown ? 1 : this.data.sortUpDown + 1) : null,
      "requestData.order": data,
    })
    this.getGoodsPage()
  },


  //更改样式
  changStyle() {
    this.setData({
      screenStyle: this.data.screenStyle + 1
    })
  },
  //筛选
  screen() { },

})